package com.wjd.wshow.service.impl;

import com.wjd.wshow.common.resource.Resource;
import com.wjd.wshow.common.resource.ResourceType;
import com.wjd.wshow.entity.Image;
import com.wjd.wshow.common.resource.entity.ImageResource;
import com.wjd.wshow.repository.ImageRepository;
import com.wjd.wshow.service.ImageService;
import com.wjd.wshow.service.ThumbnailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service("ImageService")
public class ImageServiceImpl extends ResourceServiceImpl<Image> implements ImageService {

    private static Logger logger = LoggerFactory.getLogger(ImageServiceImpl.class);

    @javax.annotation.Resource(name = "ThumbnailService")
    private ThumbnailService thumbnailService;

    @javax.annotation.Resource(name = "ImageRepository")
    private ImageRepository imageRepository;

    @Override
    public String addImage(Image image) {
        Image img = create(image);
        if (img == null) {
            logger.error("Add image failed!");
            return null;
        }
        ImageResource resource = (ImageResource) getResource(img.getId());
        if (resource != null) {
            thumbnailService.createThumbnail(resource);
        }
        logger.info("Add image: " + img.getId());
        return img.getId();
    }

    @Override
    public boolean deleteImage(String id) {
        logger.info("Delete image: " + id);
        deleteById(id);
        return true;
    }

    @Override
    public boolean deleteAllImages() {
        List<Image> images = getImages();
        for (Image image : images) {
            deleteImage(image.getId());
        }
        return true;
    }

    @Override
    public boolean updateImage(Image Image) {
        if (!getRepository().existsById(Image.getId())) {
            logger.info("Update image not exist: " + Image.getId());
            return false;
        }
        logger.info("Update image: " + Image.getId());
        update(Image);
        return true;
    }

    @Override
    public Optional<Image> getImage(String id) {
        return getRepository().findById(id);
    }

    @Override
    public List<Image> getImages() {
        List<Image> images = new ArrayList<>();
        Iterable<Image> it = getRepository().findAll();
        for (Image img : it) {
            images.add(img);
        }
        return images;
    }

    @Override
    public List<Image> findImages(String path) {
        if (path == null) {
            return new ArrayList<>(0);
        }
        return getRepository().findByPath(path);
    }

    @Override
    protected ResourceType getResourceType() {
        return ResourceType.IMAGE;
    }

    @Override
    public Resource getResource(String id) {
        ImageResource resource;
        Optional<Image> optional = getImage(id);
        if (!optional.isPresent()) {
            return null;
        }
        resource = new ImageResource(optional.get());
        return resource;
    }

    @Override
    public void deleteResource(String id) {
        thumbnailService.deleteThumbnail(id);
        super.deleteResource(id);
    }

    @Override
    public ImageRepository getRepository() {
        return imageRepository;
    }
}
